/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package times.persistence.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import times.persistence.entities.SlaughterProperty;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import times.persistence.controllers.exceptions.IllegalOrphanException;
import times.persistence.controllers.exceptions.NonexistentEntityException;
import times.persistence.entities.PropertyPriceSource;

/**
 *
 * @author Administrator
 */
public class PropertyPriceSourceJpaController implements Serializable {

    public PropertyPriceSourceJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(PropertyPriceSource propertyPriceSource) {
        if (propertyPriceSource.getSlaughterPropertyList() == null) {
            propertyPriceSource.setSlaughterPropertyList(new ArrayList<SlaughterProperty>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<SlaughterProperty> attachedSlaughterPropertyList = new ArrayList<SlaughterProperty>();
            for (SlaughterProperty slaughterPropertyListSlaughterPropertyToAttach : propertyPriceSource.getSlaughterPropertyList()) {
                slaughterPropertyListSlaughterPropertyToAttach = em.getReference(slaughterPropertyListSlaughterPropertyToAttach.getClass(), slaughterPropertyListSlaughterPropertyToAttach.getId());
                attachedSlaughterPropertyList.add(slaughterPropertyListSlaughterPropertyToAttach);
            }
            propertyPriceSource.setSlaughterPropertyList(attachedSlaughterPropertyList);
            em.persist(propertyPriceSource);
            for (SlaughterProperty slaughterPropertyListSlaughterProperty : propertyPriceSource.getSlaughterPropertyList()) {
                PropertyPriceSource oldPropertyPriceSourceIdOfSlaughterPropertyListSlaughterProperty = slaughterPropertyListSlaughterProperty.getPropertyPriceSourceId();
                slaughterPropertyListSlaughterProperty.setPropertyPriceSourceId(propertyPriceSource);
                slaughterPropertyListSlaughterProperty = em.merge(slaughterPropertyListSlaughterProperty);
                if (oldPropertyPriceSourceIdOfSlaughterPropertyListSlaughterProperty != null) {
                    oldPropertyPriceSourceIdOfSlaughterPropertyListSlaughterProperty.getSlaughterPropertyList().remove(slaughterPropertyListSlaughterProperty);
                    oldPropertyPriceSourceIdOfSlaughterPropertyListSlaughterProperty = em.merge(oldPropertyPriceSourceIdOfSlaughterPropertyListSlaughterProperty);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(PropertyPriceSource propertyPriceSource) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            PropertyPriceSource persistentPropertyPriceSource = em.find(PropertyPriceSource.class, propertyPriceSource.getId());
            List<SlaughterProperty> slaughterPropertyListOld = persistentPropertyPriceSource.getSlaughterPropertyList();
            List<SlaughterProperty> slaughterPropertyListNew = propertyPriceSource.getSlaughterPropertyList();
            List<String> illegalOrphanMessages = null;
            for (SlaughterProperty slaughterPropertyListOldSlaughterProperty : slaughterPropertyListOld) {
                if (!slaughterPropertyListNew.contains(slaughterPropertyListOldSlaughterProperty)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain SlaughterProperty " + slaughterPropertyListOldSlaughterProperty + " since its propertyPriceSourceId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<SlaughterProperty> attachedSlaughterPropertyListNew = new ArrayList<SlaughterProperty>();
            for (SlaughterProperty slaughterPropertyListNewSlaughterPropertyToAttach : slaughterPropertyListNew) {
                slaughterPropertyListNewSlaughterPropertyToAttach = em.getReference(slaughterPropertyListNewSlaughterPropertyToAttach.getClass(), slaughterPropertyListNewSlaughterPropertyToAttach.getId());
                attachedSlaughterPropertyListNew.add(slaughterPropertyListNewSlaughterPropertyToAttach);
            }
            slaughterPropertyListNew = attachedSlaughterPropertyListNew;
            propertyPriceSource.setSlaughterPropertyList(slaughterPropertyListNew);
            propertyPriceSource = em.merge(propertyPriceSource);
            for (SlaughterProperty slaughterPropertyListNewSlaughterProperty : slaughterPropertyListNew) {
                if (!slaughterPropertyListOld.contains(slaughterPropertyListNewSlaughterProperty)) {
                    PropertyPriceSource oldPropertyPriceSourceIdOfSlaughterPropertyListNewSlaughterProperty = slaughterPropertyListNewSlaughterProperty.getPropertyPriceSourceId();
                    slaughterPropertyListNewSlaughterProperty.setPropertyPriceSourceId(propertyPriceSource);
                    slaughterPropertyListNewSlaughterProperty = em.merge(slaughterPropertyListNewSlaughterProperty);
                    if (oldPropertyPriceSourceIdOfSlaughterPropertyListNewSlaughterProperty != null && !oldPropertyPriceSourceIdOfSlaughterPropertyListNewSlaughterProperty.equals(propertyPriceSource)) {
                        oldPropertyPriceSourceIdOfSlaughterPropertyListNewSlaughterProperty.getSlaughterPropertyList().remove(slaughterPropertyListNewSlaughterProperty);
                        oldPropertyPriceSourceIdOfSlaughterPropertyListNewSlaughterProperty = em.merge(oldPropertyPriceSourceIdOfSlaughterPropertyListNewSlaughterProperty);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = propertyPriceSource.getId();
                if (findPropertyPriceSource(id) == null) {
                    throw new NonexistentEntityException("The propertyPriceSource with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            PropertyPriceSource propertyPriceSource;
            try {
                propertyPriceSource = em.getReference(PropertyPriceSource.class, id);
                propertyPriceSource.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The propertyPriceSource with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<SlaughterProperty> slaughterPropertyListOrphanCheck = propertyPriceSource.getSlaughterPropertyList();
            for (SlaughterProperty slaughterPropertyListOrphanCheckSlaughterProperty : slaughterPropertyListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This PropertyPriceSource (" + propertyPriceSource + ") cannot be destroyed since the SlaughterProperty " + slaughterPropertyListOrphanCheckSlaughterProperty + " in its slaughterPropertyList field has a non-nullable propertyPriceSourceId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(propertyPriceSource);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<PropertyPriceSource> findPropertyPriceSourceEntities() {
        return findPropertyPriceSourceEntities(true, -1, -1);
    }

    public List<PropertyPriceSource> findPropertyPriceSourceEntities(int maxResults, int firstResult) {
        return findPropertyPriceSourceEntities(false, maxResults, firstResult);
    }

    private List<PropertyPriceSource> findPropertyPriceSourceEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(PropertyPriceSource.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public PropertyPriceSource findPropertyPriceSource(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(PropertyPriceSource.class, id);
        } finally {
            em.close();
        }
    }

    public int getPropertyPriceSourceCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<PropertyPriceSource> rt = cq.from(PropertyPriceSource.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
